home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / TEMP / GNU / bison / Rules < prev    next >
Text File  |  1995-06-28  |  2KB  |  80 lines

  1. Rules
  2. Previous: <Symbols=>Symbols> * Next: <Recursion=>Recursion> * Up: <Grammar File=>GrammarFil>
  3.  
  4. #Wrap on
  5. {fH3}Syntax of Grammar Rules{f}
  6.  
  7. A Bison grammar rule has the following general form:
  8.  
  9. #Wrap off
  10. #fCode
  11. {fStrong}result{f}: {fStrong}components{f}…
  12.         ;
  13. #f
  14. #Wrap on
  15.  
  16. where {fStrong}result{f} is the nonterminal symbol that this rule describes
  17. and {fStrong}components{f} are various terminal and nonterminal symbols that
  18. are put together by this rule (\*Note <Symbols=>Symbols>).  
  19.  
  20. For example,
  21.  
  22. #Wrap off
  23. #fCode
  24. exp:      exp '+' exp
  25.         ;
  26. #f
  27. #Wrap on
  28.  
  29. says that two groupings of type {fCode}exp{f}, with a {fEmphasis}+{f} token in between,
  30. can be combined into a larger grouping of type {fCode}exp{f}.
  31.  
  32. Whitespace in rules is significant only to separate symbols.  You can add
  33. extra whitespace as you wish.
  34.  
  35. Scattered among the components can be {fStrong}actions{f} that determine
  36. the semantics of the rule.  An action looks like this:
  37.  
  38. #Wrap off
  39. #fCode
  40. \{{fStrong}C statements{f}\}
  41. #f
  42. #Wrap on
  43.  
  44. Usually there is only one action and it follows the components.
  45. \*Note <Actions=>Actions>.
  46.  
  47. Multiple rules for the same {fStrong}result{f} can be written separately or can
  48. be joined with the vertical-bar character {fEmphasis}|{f} as follows:
  49.  
  50. #Wrap off
  51. #fCode
  52. {fStrong}result{f}:   {fStrong}rule1-components{f}…
  53.         | {fStrong}rule2-components{f}…
  54.         …
  55.         ;
  56. #f
  57. #Wrap on
  58.  
  59. They are still considered distinct rules even when joined in this way.
  60.  
  61. If {fStrong}components{f} in a rule is empty, it means that {fStrong}result{f} can
  62. match the empty string.  For example, here is how to define a
  63. comma-separated sequence of zero or more {fCode}exp{f} groupings:
  64.  
  65. #Wrap off
  66. #fCode
  67. expseq:   \/\* empty \*\/
  68.         | expseq1
  69.         ;
  70.  
  71. expseq1:  exp
  72.         | expseq1 ',' exp
  73.         ;
  74. #f
  75. #Wrap on
  76.  
  77. It is customary to write a comment {fEmphasis}\/\* empty \*\/{f} in each rule
  78. with no components.
  79.  
  80.